package com.example.mq.demo;

import com.example.mq.common.MqException;
import com.example.mq.mqclient.Channel;
import com.example.mq.mqclient.Connection;
import com.example.mq.mqclient.ConnectionFactory;
import com.example.mq.mqserver.core.BasicProperties;
import com.example.mq.mqserver.core.Consume;
import com.example.mq.mqserver.core.ExchangeType;

import java.io.IOException;

/**
 * 模拟消费者，一般都是独立的服务器程序
 * 这里使用单机程序
 */
public class ConsumerDemo {
    public static void main(String[] args) throws IOException, MqException {
        System.out.println("<------------------消费者启动-------------------->");
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(9999);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("exchange", ExchangeType.DIRECT,true,false,null);
        channel.queueDeclare("queue",true,false,true,null);

        channel.basicConsume(channel.getChannelId(), "queue", true, new Consume() {
            @Override
            public void handleDelivery(String consumerTag, BasicProperties basicProperties, byte[] body) {
                System.out.println("开始消费....");
                System.out.println("consumerTag=" + consumerTag);
                System.out.println("basicProperties" + basicProperties);
                System.out.println("body=" + new String(body));
                System.out.println("消费完成...");
            }
        });

        while(true) {}
    }
}
